package com.block.datapush.service.filter;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.block.datapush.model.param.FilterHandlerParam;
import com.block.datapush.model.result.FilterHandlerResult;
import com.block.datapush.service.IDataFilterItemService;
import com.block.db.entity.MemberSourceInfo;
import org.springframework.stereotype.Service;

@Service
public class FilterAgeHandlerService implements IDataFilterItemService {

    @Override
    public FilterHandlerResult handlerFilterLogic(FilterHandlerParam handlerParam, MemberSourceInfo memberSourceInfo) {

        Integer minAge = ObjectUtil.isEmpty(handlerParam.getMinAge())?20:handlerParam.getMinAge();
        Integer maxAge = ObjectUtil.isEmpty(handlerParam.getMaxAge())?60:handlerParam.getMaxAge();

        Integer curAge = memberSourceInfo.getAge();

        if (curAge >= minAge && curAge <= maxAge){
            return FilterHandlerResult.success();
        }

        String message = StrUtil.format("【filter】{}-{}【value】{}", minAge, maxAge, curAge);
        return FilterHandlerResult.error("AGE_ERROR", message);
    }
}
